﻿using System;
using System.Linq;
using UnityEngine;


namespace RStudio.UIFramwork.Utils
{
    /// <summary>
    /// 
    /// </summary>
    public class UIAssetUtil
    {
        /// <summary>
        /// 验证|脚本继承关系
        /// </summary>
        public static bool IsUIBaseAttached(Type type, GameObject uiAsset)
        {
            bool isHasUIBase = uiAsset.TryGetComponent(out UIBase _);
            if (!isHasUIBase)
            {
                Debug.LogError($"类：{type} 缺失 UIBase 继承条件，无法被实例化，请检查！");
                return false;
            }

            return true;
        }

        /// <summary>
        /// 验证|脚本类特性信息
        /// </summary>
        public static bool IsUIDetailAttached(Type type, out UIDetailAttribute output)
        {
            object match = type.GetCustomAttributes(false).ToList().Find(e => e is UIDetailAttribute);
            bool isValid = match is UIDetailAttribute;

            if (!isValid)
            {
                Debug.LogError($"类：{type} 缺失 UIDetail 的特性，无法被实例化，请检查！");
                output = null;
                return false;
            }

            output = match as UIDetailAttribute;
            return true;
        }

        /// <summary>
        /// 验证|脚本类关联路径是否合格
        /// </summary>
        /// <param name="uiDetail"></param>
        /// <returns></returns>
        public static bool IsUIDetailAssetLinkAttached(Type type, UIDetailAttribute uiDetail)
        {
            if (uiDetail.IsAssetLinkValid())
            {
                return true;
            }

            Debug.LogError($"类型：{type} .UIDetail 未添加相关路径绑定项");
            return false;
        }
    }
}
